﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharp
{
    // class that contain Calc method that calclute the distance between two vertex 
    // by taking long an lat for each vertex.
    class DistanceBet
    {
        // cal method that take lon and lat for every vertex and calculate dis between them.
        // return distance with type double.

        public double Calc(double lon1, double lat1, double lon2, double lat2)
        {
            double dlon = lon2 - lon1;
            double dlat = lat2 - lat1;
            double dDistance = Double.MinValue;

            double dLat1InRad = lat1 * (Math.PI / 180.0);
            double dLong1InRad = lon1 * (Math.PI / 180.0);
            double dLat2InRad = lat2 * (Math.PI / 180.0);
            double dLong2InRad = lon2 * (Math.PI / 180.0);

            double dLongitude = dLong2InRad - dLong1InRad;
            double dLatitude = dLat2InRad - dLat1InRad;

            // Intermediate result a.
            double a = Math.Pow(Math.Sin(dLatitude / 2.0), 2.0) +
                       Math.Cos(dLat1InRad) * Math.Cos(dLat2InRad) *
                       Math.Pow(Math.Sin(dLongitude / 2.0), 2.0);

            // Intermediate result c (great circle distance in Radians).
            double c = 2.0 * Math.Asin(Math.Sqrt(a));

            // Distance.
            // const Double kEarthRadiusMiles = 3956.0;
            const Double kEarthRadiusKms = 6376.5;
            dDistance = kEarthRadiusKms * c;
            Console.WriteLine("Distance between this two nodes are ");
            Console.WriteLine(dDistance);
            return dDistance;
        }
    }
}
